package com.itheima.test;

import com.itheima.domain.Teacher;

import java.util.ArrayList;
import java.util.Scanner;

public class TeacherManager {
    public static void main(String[] args) {
        //创建集合,用于储存老师对象
        ArrayList<Teacher> list = new ArrayList<>();
        //键入老师的信息,用Scanner实现
        Scanner sc = new Scanner(System.in);
        //功能提示
       while(true) {
           System.out.println("-------------------------");
           System.out.println("老师管理系统,具有以下功能");
           System.out.println("1.添加老师信息");
           System.out.println("2.修改老师信息");
           System.out.println("3.删除老师信息");
           System.out.println("4.查看老师信息");
           System.out.println("5.关闭系统");
           System.out.println("请输入:");
           int chioce = sc.nextInt();
           switch(chioce){
               case 1:
                   addTeacher(list);
                   break;
               case 2:
                   upDateTeacher(list);
                   break;
               case 3:
                   deleteTeacher(list);
                   break;
               case 4:
                   showTeacher(list);
                   break;
               case 5:
                   System.out.println("关闭成功"
                   );
                   return;
           }

       }



    }

    public static void showTeacher(ArrayList<Teacher> list) {
        if(list.size()==0){
            System.out.println("没有老师信息,请先添加信息");
            return;
        }else {
            System.out.println("学号\t\t姓名\t年龄\t生日");
            for (int i = 0; i < list.size(); i++) {
                Teacher tea = list.get(i);
                System.out.println(tea.getId()+"\t"+tea.getName()+"\t"+tea.getAge()+"\t\t" + tea.getBirthday());
            }
        }


    }

    public static void deleteTeacher(ArrayList<Teacher> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要删除的老师工号");
        String id = sc.next();
        int index = getIndex(list,id);
        if(index == -1){
            System.out.println("未查询到该工号,请重新输入");
        }else{
            list.remove(index);
            System.out.println("删除成功");
        }

    }

    public static void upDateTeacher(ArrayList<Teacher> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要修改的老师工号");
        String idUpDate = sc.next();
        int index = getIndex(list,idUpDate);
        if(index == -1){
            System.out.println("未查询到该工号,请重新输入");
        }else{System.out.println("请输入老师新姓名");
            String name = sc.next();
            System.out.println("请输入老师新年龄");
            int age = sc.nextInt();
            System.out.println("请输入老师新生日");
            String birthday = sc.next();
            Teacher teaNew = new Teacher(idUpDate,name,age,birthday);
            list.set(index,teaNew);
            System.out.println("修改成功");
        }
    }

    public static void addTeacher(ArrayList<Teacher> list) {
        Scanner sc =new Scanner(System.in);
        System.out.println("请输入老师工号");
        String id = sc.next();
        System.out.println("请输入老师姓名");
        String name = sc.next();
        System.out.println("请输入老师年龄");
        int age = sc.nextInt();
        System.out.println("请输入老师生日");
        String birthday = sc.next();
        Teacher tea = new Teacher(id,name,age,birthday);
        list.add(tea);
        System.out.println("添加成功");
    }

    public static int getIndex(ArrayList<Teacher> list,String id){
        int index = -1;

        for (int i = 0; i < list.size(); i++) {
            if(id.equals(list.get(i).getId())){
                index = i;

            }
        }
        return index;

    }
}
